<template>
  <div class="app-container">
    <el-button type="success" icon="el-icon-finished" style="margin: 10px;" @click="handleSubmit">
      提交
    </el-button>
    <el-button type="info" icon="el-icon-refresh" style="margin: 10px;" @click="handleRefresh">
      重置
    </el-button>
    <el-button type="info" icon="el-icon-circle-close" style="margin: 10px;" @click="handleClose">
      关闭
    </el-button>
    <el-button type="danger" icon="el-icon-delete" style="margin: 10px;" @click="handleDelete">
      删除
    </el-button>

    <el-form ref="helpEditForm" :rules="helpRules" :model="help" label-width="70px" style="margin-left:0px;">
      <el-form-item label="分类" prop="category">
        <el-select v-model="help.category" clearable>
          <el-option v-for="item in categoryOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="help.status" clearable>
          <el-option v-for="item in statusOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="help.title" clearable style="width: 400px;" />
      </el-form-item>
      <el-form-item label="正文" prop="content">
        <el-input v-model="help.content" type="textarea" :rows="4" style="width: 400px;" />
      </el-form-item>
      <el-form-item label="发布时间" prop="publishedTime">
        <el-date-picker
          v-model="help.publishedTime"
          type="datetime"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder=""
        />
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import { Message } from 'element-ui'
import { fetchEnum } from '@/api/system-api'
import { getHelpById, putHelpById, deleteHelpById } from '@/api/manager/info-api'
import { toOptionsList } from '@/utils/index'

export default {
  name: 'ManagerHelpEdit',
  filters: {
  },
  data() {
    return {
      categoryOptions: [],
      statusOptions: [],
      help: {
        id: null,
        category: null,
        status: null,
        title: null,
        content: null,
        publishedTime: null
      },
      helpRules: {
        category: [{
          required: true, message: '请输入分类', trigger: 'blur'
        }],
        status: [{
          required: true, message: '请输入状态', trigger: 'blur'
        }],
        title: [{
          required: true, message: '请输入标题', trigger: 'blur'
        }]
      }
    }
  },
  async created() {
    await this.initLoad()
    await this.loadDetail()
  },
  methods: {
    async initLoad() {
      const dataDictsParam = ['HELP_CENTER_HELP_CATEGORY', 'HELP_CENTER_HELP_STATUS']
      const { data } = await fetchEnum(dataDictsParam)
      for (const dataDict of data) {
        if (dataDict.code === 'HELP_CENTER_HELP_CATEGORY') {
          if (dataDict.options) {
            this.categoryOptions = toOptionsList(dataDict.options, 'code', 'name')
          }
        }
        if (dataDict.code === 'HELP_CENTER_HELP_STATUS') {
          if (dataDict.options) {
            this.statusOptions = toOptionsList(dataDict.options, 'code', 'name')
          }
        }
      }
    },
    loadDetail() {
      getHelpById(this.$route.params.id).then(response => {
        const { data } = response
        this.help = data
        if (this.help) {
          this.help.category = '' + this.help.category
          this.help.status = '' + this.help.status
        }
      })
    },

    handleSubmit() {
      this.$refs.helpEditForm.validate((valid) => {
        if (!valid) {
          return
        }
        var data = JSON.parse(JSON.stringify(this.help))
        putHelpById(this.help.id, data).then(response => {
          this.$notify({
            title: '成功',
            message: '更新成功',
            type: 'success',
            duration: 1 * 1000
          })
        }).catch(error => {
          Message({
            message: error,
            type: 'error',
            duration: 5 * 1000
          })
        })
      })
    },
    handleRefresh() {
      this.loadDetail()
    },
    handleClose() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    },
    handleDelete() {
      deleteHelpById(this.help.id).then(response => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 1 * 1000
        })
        this.handleClose()
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
      })
    }

  }
}

</script>
